#include<bits/stdc++.h>
using namespace std;

struct node {
	int x, y, h;
};

int tmp[505][505];
int len[505][505];

signed main() {
	int n, m;
	cin >> n >> m;

	vector<node> a;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> tmp[i][j];
			a.push_back({i, j, tmp[i][j]});
		}
	}

	sort(a.begin(), a.end(), [](node x, node y) {
		return x.h < y.h;
	});

	for (int i = 0; i < a.size(); i++) {
		int x = a[i].x;
		int y = a[i].y;
		int h = a[i].h;

		if (tmp[x - 1][y] > h)
			len[x - 1][y] = max(len[x - 1][y], len[x][y] + 1);

		if (tmp[x + 1][y] > h)
			len[x + 1][y] = max(len[x + 1][y], len[x][y] + 1);

		if (tmp[x][y - 1] > h)
			len[x][y - 1] = max(len[x][y - 1], len[x][y] + 1);

		if (tmp[x][y + 1] > h)
			len[x][y + 1] = max(len[x][y + 1], len[x][y] + 1);
	}

	cout << *max_element(&len[0][0], &len[n][m + 1]) + 1;
	return 0;
}
